<template>
  <div class="farm-list">
    <a-card title="已接入设备列表">
      <a-table :columns="columns" :data-source="data"></a-table>
    </a-card>
  </div>
</template>

<script>
export default {
  name: "FarmList",
  data() {
    return {
      columns: [
        { key: "code", title: "农场编码", dataIndex: "code" },
        { key: "name", title: "农场名称", dataIndex: "name" },
        { key: "location", title: "地区", dataIndex: "location" },
        { key: "industry", title: "行业", dataIndex: "industry" },
        { key: "encodeType", title: "编码类型", dataIndex: "encodeType" },
        { key: "networkIdentifier", title: "网络标识", dataIndex: "networkIdentifier" },
        { key: "gbId", title: "国标ID", dataIndex: "gbId" },
      ],
      data: [],
    };
  },
  created() {
    this.listFarm();
  },
  methods: {
    async listFarm() {
      const { data: data } = await this.$http.get('/farm')
      // const objMap = this.$store.getters.govRegionMap
      // console.log(objMap)
      this.data = []
      data.forEach(farm => {
        this.data.push({
          key: farm.id,
          code: farm.farmCode,
          name: farm.farmAlias,
          location: `${this.getLocationValue(farm.province)}\\${this.getLocationValue(farm.city)}\\${this.getLocationValue(farm.distinct)}`,
          industry: this.getIndustryValue(farm.industry),
          encodeType: this.getTypeValue(farm.encodeType),
          networkIdentifier: this.getnetworkValue(farm.networkIdentifier),
          gbId: farm.gbIdStart
        })
      })
    },
    getLocationValue(key) {
      let locationMap = this.$store.getters.locationMap
      if (locationMap.has(key)) {
        return locationMap.get(key).name
      } else {
        return ''
      }
    },
    getTypeValue(key){
      let typeList = this.$store.getters.typeSelection
      let idx = typeList.findIndex(x=> key === x.value)
      return idx > 0 ? typeList[idx].label : ""
    },
    getIndustryValue(key) {
      let industryList = this.$store.getters.industryOptions
      let idx = industryList.findIndex(x=> key === x.value)
      return idx > 0 ? industryList[idx].label : ""
    },
    getnetworkValue(key) {
      let networkList = this.$store.getters.networkOptions
      let idx = networkList.findIndex(x=> key === x.value)
      return idx > 0 ? networkList[idx].label : ""
    }
  },
};
</script>

<style scoped>
.ant-card-head-title {
  flex: none;
}
</style>